import mongoose from 'mongoose'
const Schema = mongoose.Schema
const out_of_china = function(e, t) {
  return e < 72.004 || e > 137.8347 || t < .8293 || t > 55.8271 || !1
}

const transformlat = function(e, t) {
  var n = 3.141592653589793,
  i = 2 * e - 100 + 3 * t + .2 * t * t + .1 * e * t + .2 * Math.sqrt(Math.abs(e));
  return i += 2 * (20 * Math.sin(6 * e * n) + 20 * Math.sin(2 * e * n)) / 3,
  (i += 2 * (20 * Math.sin(t * n) + 40 * Math.sin(t / 3 * n)) / 3) + 2 * (160 * Math.sin(t / 12 * n) + 320 * Math.sin(t * n / 30)) / 3
}
const transformlng = function(e, t) {
  var n = 3.141592653589793,
  i = 300 + e + 2 * t + .1 * e * e + .1 * e * t + .1 * Math.sqrt(Math.abs(e));
  return i += 2 * (20 * Math.sin(6 * e * n) + 20 * Math.sin(2 * e * n)) / 3,
  (i += 2 * (20 * Math.sin(e * n) + 40 * Math.sin(e / 3 * n)) / 3) + 2 * (150 * Math.sin(e / 12 * n) + 300 * Math.sin(e / 30 * n)) / 3
}



const YaolingSchema = new Schema({
  gentime: Number,
  latitude: {type:Number,unique:true,dropDups: true},
  lifetime: Number,
  longtitude: {type:Number,unique:true,dropDups: true},
  sprite_id: Number,
  // point: String,
  request: {
    type: Number,
    default: 0
  }
})

let valid = YaolingSchema.virtual('valid')

valid.get(function() {
  let time = new Date((this.gentime + this.lifetime) * 1000) - new Date()
  console.log(`有效时间: ${time}`)
  time = Math.floor(time/ 1000)
  // let second = time / 1000
  // let minute = Math.floor(second / 60)
  // second = Math.floor(second % 60)
  return time
})

YaolingSchema.virtual('point').get(function() {
  let longtitude = this.longtitude / 1e6
  let latitude = this.latitude / 1e6
  var n = 3.141592653589793,
  i = 6378245,
  r = .006693421622965943;
  if (out_of_china(longtitude, latitude)) return [longtitude, latitude];
  var a = transformlat(longtitude - 105, latitude - 35),
  o = transformlng(longtitude - 105, latitude - 35),
  s = latitude / 180 * n,
  l = Math.sin(s);
  l = 1 - r * l * l;
  var u = Math.sqrt(l);
  return a = 180 * a / (i * (1 - r) / (l * u) * n),
  [2 * longtitude - (longtitude + (o = 180 * o / (i / u * Math.cos(s) * n))), 2 * latitude - (latitude + a)]
})






YaolingSchema.pre('save', function(next) {

  next()
})

mongoose.model('Yaoling', YaolingSchema)